/**
 * @file lv_appicon.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_appicon.h"
#include "../../widgets/lv_obj_ex/lv_obj_ex.h" /*lv_obj_delete_all_child()*/

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

static lv_style_t style_pressed;
static lv_style_t style_focused;
static uint32_t style_pressed_used_count = 0;
static uint32_t style_focused_used_count = 0;
/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_appicon_create(lv_obj_t * obj, const lv_appicon_info_t *info, const lv_appicon_cfg_t *cfg, lv_event_cb_t event_cb)
{
    if(cfg->style_pressed == NULL){
        if(style_pressed_used_count == 0){
            lv_style_init(&style_pressed);
            lv_style_set_transform_width(&style_pressed, -10);
            lv_style_set_transform_height(&style_pressed, -10);
            lv_style_set_opa(&style_pressed, LV_OPA_70);
        }
        style_pressed_used_count++;
    }
    if(!(cfg->flag & LV_APPICON_DIS_STYLE_FOCUSED) && (cfg->style_focused == NULL)){
        if(style_focused_used_count == 0){
            lv_style_init(&style_focused);
            lv_style_set_bg_opa(&style_focused, LV_OPA_30);
            lv_style_set_bg_color(&style_focused, lv_palette_main(LV_PALETTE_BLUE_GREY));
            lv_style_set_border_width(&style_focused, 2);
            lv_style_set_border_opa(&style_focused, LV_OPA_50);
        }
        style_focused_used_count++;
    }

    lv_obj_t * img = lv_img_create(obj);
    lv_appicon_rebuild(obj, info, cfg, event_cb);
}

void lv_appicon_rebuild(lv_obj_t * obj, const lv_appicon_info_t *info, const lv_appicon_cfg_t *cfg, lv_event_cb_t event_cb)
{
    /*rebuild img*/
    lv_obj_t * img = lv_obj_get_child(obj, 0);
    if(img == NULL) return;
    if((void*)info != lv_obj_get_user_data(img)){/*changed*/
        lv_obj_set_user_data(img, (void*)cfg);
        lv_obj_add_flag(img, LV_OBJ_FLAG_FLOATING);
        lv_image_header_t header;
        lv_result_t res = lv_image_decoder_get_info(info->icon_path, &header);
        if(res == LV_RESULT_OK) lv_img_set_src(img, info->icon_path);
        else{
            lv_img_set_src(img, LV_SYMBOL_IMAGE);
            uint16_t zoom = cfg->flag & LV_APPICON_UNIMAGE_SCALE_MASK;
            zoom = (zoom + 1) * 24;
            lv_obj_set_size(img, zoom, zoom);
        }
        lv_obj_align(img, LV_ALIGN_TOP_MID, 0, cfg->icon_offset);
        lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_GESTURE_BUBBLE);
        if(event_cb != NULL) lv_obj_add_event_cb(img, event_cb, LV_EVENT_ALL, (void*)info);
        if(cfg->style_pressed) lv_obj_add_style(img, cfg->style_pressed, LV_STATE_PRESSED);
        else lv_obj_add_style(img, &style_pressed, LV_STATE_PRESSED);
        if(!(cfg->flag & LV_APPICON_DIS_STYLE_FOCUSED)){
            if(cfg->style_focused) lv_obj_add_style(img, cfg->style_focused, LV_STATE_FOCUSED);
            else lv_obj_add_style(img, &style_focused, LV_STATE_FOCUSED);
        }

        lv_obj_invalidate(img);
    }
    /*rebuild name*/
    lv_obj_t * name = lv_obj_get_child(obj, 1);
    if(!(cfg->flag & LV_APPICON_DIS_NAME)){/*enable name display*/
        if(name == NULL) name = lv_label_create(obj);
        else if((void*)cfg != lv_obj_get_user_data(name)) return; /*unchanged*/
    }
    else{
      if(name != NULL) lv_obj_delete(name);
      return;
    }
    lv_obj_add_flag(name, LV_OBJ_FLAG_FLOATING);
    lv_label_set_text(name, info->icon_name);
    if(cfg->name_font != NULL)
        lv_obj_set_style_text_font(name, cfg->name_font, 0);
    if(!(cfg->flag & LV_APPICON_DIS_NAME_FG_COLOR))
        lv_obj_set_style_text_color(name, lv_color_hex(cfg->name_fg_color), 0);
    if(cfg->flag & LV_APPICON_EN_NAME_BG_COLOR){
        lv_obj_set_style_bg_color(name, lv_color_hex(cfg->name_bg_color), 0);
        lv_obj_set_style_bg_opa(name, LV_OPA_20, 0);
    }

    lv_obj_align(name, LV_ALIGN_BOTTOM_MID, 0, -cfg->name_offset);
    lv_obj_invalidate(name);
}

void lv_appicon_add_focused(lv_obj_t * obj)
{
    lv_obj_t * img = lv_obj_get_child(obj, 0);
    if(img == NULL) return;
    lv_obj_add_state(img, LV_STATE_FOCUSED);
}
void lv_appicon_remove_focused(lv_obj_t * obj)
{
    lv_obj_t * img = lv_obj_get_child(obj, 0);
    if(img == NULL) return;
    lv_obj_remove_state(img, LV_STATE_FOCUSED);
}

void lv_appicon_delete(lv_obj_t * obj)
{
 lv_obj_t * img = lv_obj_get_child(obj, 0);
  const lv_appicon_cfg_t *cfg = lv_obj_get_user_data(img);
  if(cfg && !cfg->style_pressed){
    if(style_pressed_used_count){
        style_pressed_used_count--;
        if(style_pressed_used_count == 0){
            lv_style_reset(&style_pressed);
        }
    }
  }
  if(cfg && !(cfg->flag & LV_APPICON_DIS_STYLE_FOCUSED) && !cfg->style_focused){
    if(style_focused_used_count){
        style_focused_used_count--;
        if(style_focused_used_count == 0){
            lv_style_reset(&style_focused);
        }
    }
    lv_obj_delete_all_child(obj);
  }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
